package rua.jose.macamo;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class XMLHandlerSpot extends DefaultHandler {
   StringBuilder currentValue = new StringBuilder();
   ArrayList<Spot> spots; //Holds the spots parsed so far
   String imageFile; //Holds the name of the imagefile

   /** Called when tag starts ( ex:- <name>AndroidPeople</name>http://cs.joensuu.fi/~pparvia/railway.png
    * -- <name> )*/
   @Override
   public void startElement(final String uri, final String localName, final String qName,
         final Attributes attributes) throws SAXException {
      if (localName.equalsIgnoreCase("xml")) { //xml start tag:
         spots = new ArrayList<Spot>();
      }
      else if (localName.equalsIgnoreCase("spot")) { //new spot
         final float x = Float.parseFloat(attributes.getValue("x"));
         final float y = Float.parseFloat(attributes.getValue("y"));
         spots.add(new Spot(x,y));
      }
   }

   /** Called when tag closing ( ex:- <name>AndroidPeople</name>
    * -- </name> )*/
   @Override
   public void endElement(final String uri, final String localName, final String qName)
         throws SAXException {
      /** set value */
      if (localName.equalsIgnoreCase("image")) {
         imageFile = currentValue.toString().trim();
      }
      currentValue.delete(0, currentValue.length());
   }

   /** Called to get tag characters ( ex:- <name>AndroidPeople</name>
    * -- to get AndroidPeople Character ) */
   @Override
   public void characters(final char[] ch, final int start, final int length)
         throws SAXException {
      currentValue.append(ch, start, length);
   }
}